package com.typing.battle.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class C2dmUtil {	
	public static final String GOOGLE_C2DM_SERVER_URL = "https://android.apis.google.com/c2dm/send";
	public static final String GOOGLE_LOGIN_URL = "https://www.google.com/accounts/ClientLogin";
	
	public static void sendMessageTest(String regId, String msg, String phoneNum)
			throws Exception {
		StringBuffer postDataBuilder = new StringBuffer();

		postDataBuilder.append("registration_id=" + regId); // 등록ID
		postDataBuilder.append("&collapse_key=1");
		postDataBuilder.append("&delay_while_idle=1");
		postDataBuilder.append("&data.type=0"); // 태울
		postDataBuilder.append("&data.msg=" + URLEncoder.encode(msg, "UTF-8")); // 태울
		postDataBuilder.append("&data.from=" + phoneNum); // 태울
																				// 메시지

		byte[] postData = postDataBuilder.toString().getBytes("UTF8");

		URL url = new URL(GOOGLE_C2DM_SERVER_URL);

		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Type",
				"application/x-www-form-urlencoded");
		conn.setRequestProperty("Content-Length",
				Integer.toString(postData.length));
		conn.setRequestProperty("Authorization", "GoogleLogin auth="
				+ getAuthToken());

		OutputStream out = conn.getOutputStream();
		out.write(postData);
		out.flush();
		out.close();

		conn.getInputStream();
	}
	public static String getAuthToken() throws Exception {
		String authtoken = "";

		StringBuffer postDataBuilder = new StringBuffer();
		postDataBuilder.append("accountType=HOSTED_OR_GOOGLE"); // 똑같이 써주셔야 합니다.
		postDataBuilder.append("&Email=kukbab1@gmail.com"); // 개발자 구글 id
		postDataBuilder.append("&Passwd=rnrqkq21"); // 개발자 구글 비빌번호
		postDataBuilder.append("&service=ac2dm");
		postDataBuilder.append("&source=test-1.0");

		byte[] postData = postDataBuilder.toString().getBytes("UTF8");

		URL url = new URL(GOOGLE_LOGIN_URL);

		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Type",
				"application/x-www-form-urlencoded");
		conn.setRequestProperty("Content-Length",
				Integer.toString(postData.length));

		OutputStream out = conn.getOutputStream();
		out.write(postData);
		out.close();

		BufferedReader br = new BufferedReader(new InputStreamReader(
				conn.getInputStream()));

		String sidLine = br.readLine();
		String lsidLine = br.readLine();
		String authLine = br.readLine();

		System.out.println("sidLine----------->>>" + sidLine);
		System.out.println("lsidLine----------->>>" + lsidLine);
		System.out.println("authLine----------->>>" + authLine);
		System.out.println("AuthKey----------->>>"
				+ authLine.substring(5, authLine.length()));

		authtoken = authLine.substring(5, authLine.length());

		return authtoken;
	}
	public static void getRegistrationId(Context context) {		
		Intent registrationIntent = new Intent(
				"com.google.android.c2dm.intent.REGISTER");
		registrationIntent.putExtra("app",
				PendingIntent.getBroadcast(context, 0, new Intent(), 0)); // 어플리케이션ID
		registrationIntent.putExtra("sender", "kukbab1@gmail.com"); // 개발자ID
		//registrationIntent.setPackage("com.typing.battle");
		context.startService(registrationIntent); // 서비스 시작(등록ID발급받기)
			// 위에서 지정한 "app"와 "sender"은 맘대로 지정하시는게 아니라 구글에서 필요한 변수명들입니다.		
	}
}
